<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
$shop=plugin('jy_weishop')->plugin_setting('shop');
$return['shopset']=$shop;
$keywords=plugin('jy_weishop')->plugin_setting('search');
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['id']);
	$verifycode=$_RQ['verifycode'];
	if (empty($verifycode)) {
			exi('请输入核销码!','error','jy_aligroup/verify.index/');
	}
	if($orderid){
		$order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$uniacid,'id'=>$orderid));
	}else{
		$order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$uniacid,'id'=>$verifycode));
	}
	if (empty($order)) 
	{
		exi('核销码错误,无权核销','error','jy_aligroup/verify.index/');   
	}
	//商品独立核销门店：
	$prams=array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$storeids=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$uniacid,'id'=>$order['goodsid']),'storeids');
	$storeids=$storeids?json_decode($storeids,true):array();
	if($storeids && count($storeids)>0){
		foreach($storeids as $sv){
			$prams['storeid']=$sv['id'];
			$saler=pdo_get('jy_weishop_store_worker',$prams);
			if($saler){
				break;
			}
		}
	}else{
		//核销店员身份
		if($order['storeid']){
			//$prams['storeid']=$order['storeid'];
		}
		$saler=pdo_get('jy_weishop_store_worker',$prams);
	}
	if (empty($saler)) 
	{
		exi('您不是该店的核销员或不属于该商品的核销门店,无权核销！','error','jy_aligroup/verify.index/');   
	}
	if (empty($order)) {
			exi('订单不存在!','error','jy_aligroup/verify.index/');
	}
	$verifynum=$this->pdo_count('groups_verify',array('orderid'=>$order['id']));
	if ($verifynum>=$order['verifynum']) {	
		exi('该核销码已被使用!','error','jy_aligroup/verify.index/');		
	}
	$order_goods=pdo_getall('jy_weishop_goods',array('id'=>$order['goodsid']));
	foreach($order_goods as &$item){
		$item['total']=1;
	}
	$return['post']=array(
		'orderid'=>$order['id'],
		'verify_code'=>$verifycode,
	);
	$return['order']=$order;
	$return['order_goods']=$order_goods;
	set_html('title','核销');
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$verify_code=trim($post['verify_code']);
	$this->model('order')->order_verify($verify_code,$_SESSION['uid']);
	exi('核销成功！','','jy_aligroup/verify.index/');
	
}